package smlab.view.ui.alert
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.InteractiveObject;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import smlab.utils.DisplayObjectUtil;
	import smlab.utils.PopUpUtils;
	
	/**
	 * 提示框
	 * @author simon
	 * 
	 */
	public class Alert_SM extends Sprite
	{
		public static const ALIGN_TOP:uint=0;
		public static const ALIGN_BOTTOM:uint=1;
		public static const ALIGN_LIFT:uint=2;
		public static const ALIGN_RIGHT:uint=3;
		public static const ALIGN_CENTER:uint=4;
		public static var skinClass_Static:Class;
		public static var container_Static:DisplayObjectContainer;//全局初始化的容器（临时设置的优先于全局的）
		private static var TOP_GAP:Number=45;//文字或ICON与BG顶部的间距
		private static var AROUND_GAP:Number=20;//文字或ICON与BG四边的间距（除了顶部）
		private static var BTN_W_GAP:Number=20;//按钮的间距(最多4个按钮)
		private static var txtPoint_:Point;//内容文本框坐标
		private static var txtW_:Number;//内容文本框宽度（高度会随着文字多少而变，宽度不会变）
		private static var titleTxtAlign_:uint;//内容文本框宽度（高度会随着文字多少而变，宽度不会变）
		private static var titleTxtPoint_:Point;//内容文本框宽度（高度会随着文字多少而变，宽度不会变）
		
		private var skinClass_:Class;
		private var container_:DisplayObjectContainer;//临时设置的容器（临时设置的优先于全局的）
		private var skin_:DisplayObjectContainer;
		private var btnContainer_:Sprite;
		private var closeBtn:InteractiveObject;
		private var BG:Sprite;//默认背景
		private var defaultBGColor_:uint=0xc5c5c5;//默认背景颜色
		private var defaultAlpha_:Number=1;//窗体透明度
		private var btnArr_:Array;//按钮数组(按钮命名索引从0开始，比如：btn0)(最多4个按钮)
		private var btnTxtArr_:Array;//数组：每个按钮上的文本(最多4个按钮)
		private var btnFunArr_:Array;//数组：每个按钮点击后执行的方法(最多4个按钮)
		private var closeFun_:Function;//Alert 上的关闭按钮被点击时执行的方法
		private var icon_:DisplayObject;//Alert 中显示在内容文字上方（或者下方）的图片，图片将居中显示
		private var iconAlign_:uint=ALIGN_TOP;//图片显示的位置
		private var dragEnable_:Boolean=false;//是否可拖动
		private var isMask_:Boolean=false;//是否弹出的时候遮挡下层不让点击
		private var showCloseBtn_:Boolean=true;//是否显示关闭按钮
		private var useDefaultSkin_:Boolean=false;//是否用程序绘制一个默认皮肤
		private var defaultBtnColor_:uint=0x30758F;
		
		private var txt:TextField;//内容文本框
		private var str_:String="";//内容文字
		private var titleStr_:String="";//内容文字
		private var txtSize_:uint=12;//内容文字大小
		private var txtFont_:String="宋体";//内容文字字体
		private var txtColor_:uint=0xffffff;//内容文本颜色
		private var txtSelectEnable_:Boolean=false;//标题文本颜色
		private var txtAutoSize_:String=TextFieldAutoSize.CENTER;//内容文字文字对齐方式
		private var titleTxt:TextField;//标题文本框
		private var titleTxtFont_:String="宋体";//标题文本字体
		private var titleTxtSize_:uint=12;//标题文字大小
		private var titleTxtColor_:uint=0xffffff;//标题文字
		private var iconRegPoint:Point;//Icon 的注册点
		
		/**
		 * 构造函数
		 * @param str				内容文字
		 * @param titleStr			标题文字
		 * @param btnTxtArr			各个按钮上的文字
		 * @param btnFunArr			各个按钮点击后执行的方法
		 * @param closeFun			点击“关闭”按钮执行的方法
		 * @param container			提示框的父级容器
		 * @param icon				提示框上显示的图片
		 * @param iconAlign			提示框上的图片的对齐方式
		 * @param dragEnable		是否可以拖动
		 * @param showCloseBtn		是否显示关闭按钮
		 * @param isMask			是否显示一层遮罩以遮挡
		 * @param txtSelectEnable	内容文本是否可选
		 * @param useDefaultSkin	是否使用默认文本
		 * @param skinClass			皮肤的类
		 * 
		 */
		public function Alert_SM(str:String="内容文字",
								 titleStr:String="系统提示",
								 btnTxtArr:Array=null,
								 btnFunArr:Array=null,
								 closeFun:Function=null,
								 container:DisplayObjectContainer=null,
								 icon:DisplayObject=null,
								 iconAlign:uint=ALIGN_TOP,
								 dragEnable:Boolean=false,
								 showCloseBtn:Boolean=true,
								 isMask:Boolean=false,
								 txtSelectEnable:Boolean=false,
								 useDefaultSkin:Boolean=false,
								 skinClass:Class=null)
		{
			str_=str;
			titleStr_=titleStr;
			btnTxtArr_=btnTxtArr;
			btnFunArr_=btnFunArr;
			closeFun_=closeFun;
			icon_=icon;
			iconAlign_=iconAlign;
			dragEnable_=dragEnable;
			showCloseBtn_=showCloseBtn;
			isMask_=isMask;
			txtSelectEnable_=txtSelectEnable;
			skinClass_=skinClass;
			
			useDefaultSkin_=useDefaultSkin;
			btnArr_=[];
			
			if(useDefaultSkin_){
				initDefaultSkin();
			}else{
				initUI();
			}
			
			addChild(skin_);
			
			if(container_Static==null){
				if(container!=null){
					container_=container;
				}else{
					container_=container_Static;
				}
				container_.addChild(this);
			}else{
				container_Static.addChild(this);
			}
			
			PopUpUtils.centerInStage(this);
			
			if(dragEnable_){
				if(BG){
					BG.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
					BG.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
				}
			}
			
			if(isMask_){
				DisplayObjectUtil.drawMaskBG(this);
			}
			
			this.cacheAsBitmap=true;
		}
		
		/**
		 * @param str				内容文字
		 * @param titleStr			标题文字
		 * @param btnTxtArr			各个按钮上的文字
		 * @param btnFunArr			各个按钮点击后执行的方法
		 * @param closeFun			点击“关闭”按钮执行的方法
		 * @param container			提示框的父级容器
		 * @param icon				提示框上显示的图片
		 * @param iconAlign			提示框上的图片的对齐方式
		 * @param dragEnable		是否可以拖动
		 * @param showCloseBtn		是否显示关闭按钮
		 * @param isMask			是否显示一层遮罩以遮挡
		 * @param txtSelectEnable	内容文本是否可选
		 * @param useDefaultSkin	是否使用默认文本
		 * @param skinClass			皮肤的类
		 * 
		 */
		public static function show(str:String="内容文字",
							 titleStr:String="系统提示",
							 btnTxtArr:Array=null,
							 btnFunArr:Array=null,
							 closeFun:Function=null,
							 container:DisplayObjectContainer=null,
							 icon:DisplayObject=null,
							 iconAlign:uint=ALIGN_TOP,
							 dragEnable:Boolean=false,
							 showCloseBtn:Boolean=true,
							 isMask:Boolean=false,
							 txtSelectEnable:Boolean=false,
							 useDefaultSkin:Boolean=false,
							 skinClass:Class=null
		):Alert_SM{
			var a:Alert_SM=new Alert_SM(str,titleStr,btnTxtArr,btnFunArr,closeFun,container,
				icon,iconAlign,dragEnable,showCloseBtn,isMask,txtSelectEnable,useDefaultSkin,skinClass);
			return a;
		}
		
		/**
		 * 初始化 Alert 的参数
		 * @param skinClass			系统皮肤类（也可以实时传不同的皮肤类）
		 * @param container			提示框的父级容器
		 * @param txtPoint			内容文本的坐标
		 * @param titleTxtAlign		标题文本的对齐方式
		 * @param titleTxtPoint		标题文本的坐标
		 * @param txtW				内容文本的默认宽度
		 * @param topGap			顶部间距
		 * @param aroundGap			左、右、下的边距
		 * @param btnWGap			按钮横向间距
		 * 
		 */
		public static function init(skinClass:Class,
							 container:DisplayObjectContainer,
							 txtPoint:Point=null,
							 titleTxtAlign:uint=ALIGN_LIFT,
							 titleTxtPoint:Point=null,
							 txtW:Number=220,
							 topGap:Number=45,
							 aroundGap:Number=20,
							 btnWGap:Number=32
		):void{
			titleTxtAlign_=titleTxtAlign;
			titleTxtPoint_=titleTxtPoint;
			txtPoint_=txtPoint;
			txtW_=txtW;
			
			if(txtPoint_){
				AROUND_GAP=txtPoint_.x;
				TOP_GAP=txtPoint_.y;
			}else{
				AROUND_GAP=aroundGap;
				TOP_GAP=topGap;
			}
			
			BTN_W_GAP=btnWGap;
			skinClass_Static=skinClass;
			container_Static=container;
		}
		
		private function initUI():void
		{
			if(skinClass_==null){
				skin_=new skinClass_Static();
			}else{
				skin_=new skinClass_;
			}
			
			BG=skin_["bg"];
			txt=skin_["txt"];
			titleTxt=skin_["titleTxt"];
			addIcon();
			addTxt();
			
			closeBtn=skin_.getChildByName("closeBtn") as InteractiveObject;
			
			if(closeBtn){
				closeBtn.visible=showCloseBtn_;
				
				if(showCloseBtn_){
					if(closeBtn is SimpleButton){
					}else{
						Sprite(closeBtn).buttonMode=true;
					}
					closeBtn.addEventListener(MouseEvent.CLICK,onClose);
				}else{
					closeBtn.removeEventListener(MouseEvent.CLICK,onClose);
				}
			}
			
			resize();
			
			btnContainer_=new Sprite;
			skin_.addChild(btnContainer_);
			
			for(var i:int=0;i<btnTxtArr_.length;i++)
			{
				var b:InteractiveObject=skin_["btn"+i];
				
				if(b){
					btnArr_.push(b);
					
					if(b is SimpleButton){
					}else{
						Sprite(b).buttonMode=true;
					}
					
					if(b["txt"]!=null){
						b["txt"].mouseEnabled=false;
						b["txt"].autoSize=TextFieldAutoSize.CENTER;
						b["txt"].htmlText="<P ALIGN='CENTER'>"+btnTxtArr_[i]+"</P>";
					}
					
					setBtnY(b);
					btnContainer_.addChild(b);
					b.addEventListener(MouseEvent.CLICK,onBtnClick);
				}
			}
			
			PopUpUtils.X_CenterInParent(btnContainer_);
		}
		
		private function initDefaultSkin(txtColor:uint=0xffffff,
									   titleTxtColor:uint=0xffffff,
									   txtSelectEnable:Boolean=false,
									   txtW:Number=0,
									   txtH:Number=16,
									   txtAutoSize:String=TextFieldAutoSize.CENTER,
									   defaultBGColor:uint=0x60ABC8,
									   defaultAlpha:Number=1,
									   txtSize:uint=12,
									   titleTxtSize:uint=12,
									   txtFont:String="宋体",
									   titleTxtFont:String="宋体"
		):void
		{
			if(skin_){
				return;
			}
			
			skin_=new Sprite;
			txtFont_=txtFont;
			txtColor_=txtColor;
			defaultAlpha_=defaultAlpha;
			titleTxtColor_=titleTxtColor;
			defaultBGColor_=defaultBGColor;
			txtSize_=txtSize;
			titleTxtSize_=titleTxtSize;
			txtAutoSize_=txtAutoSize;
			titleTxtFont_=titleTxtFont;
			addIcon();
			addTxt();
			
			if(showCloseBtn_){
				createDefaultCloseBtn();
			}
			
			resize();
			
			btnContainer_=new Sprite;
			skin_.addChild(btnContainer_);
			createAllDefaultBtn();
		}
		
		private function createAllDefaultBtn():void
		{
			DisplayObjectUtil.removeAllChild(btnContainer_);
			
			for(var i:int=0;i<btnTxtArr_.length;i++){
				var b:MovieClip=createOneDefaultBtn();
				setDefaultBtnTxt(InteractiveObject(b),btnTxtArr_[i]);
				
				if(btnTxtArr_.length==2){
					b.x=i*BTN_W_GAP+b.width*i;
				}else{
					b.x=i*BTN_W_GAP+(4-btnTxtArr_.length)*BTN_W_GAP*i+b.width*i;
				}
				
				btnContainer_.addChild(b);
				btnArr_.push(b);
				setBtnY(b);
				b.addEventListener(MouseEvent.CLICK,onBtnClick);
			}
			
			PopUpUtils.X_CenterInParent(btnContainer_);
		}
		
		private function addIcon():void
		{
			if(icon_){
				skin_.addChild(icon_);
			}
		}
		
		private function createDefaultCloseBtn():void
		{
			var s:Sprite=new Sprite;
			s.graphics.lineStyle(1.5,0xffffff);
			s.graphics.beginFill(defaultBtnColor_);
			s.graphics.drawRoundRect(0,0,20,20,4);
			s.graphics.moveTo(2,2);
			s.graphics.lineTo(18,18);
			s.graphics.moveTo(18,2);
			s.graphics.lineTo(2,18);
			s.graphics.endFill();
			s.buttonMode=true;
			skin_.addChild(s);
			closeBtn=s;
			closeBtn.addEventListener(MouseEvent.CLICK,onClose);
		}
		
		private function setBtnY(b:Object):void
		{
			if(BG){
				b.y=BG.height-b.height-22;
			}
		}
		
		private function addTxt():void
		{
			if(useDefaultSkin_)
			{
				txt=new TextField;
				titleTxt=new TextField;
				
				var tfm2:TextFormat=new TextFormat;
				tfm2.size=titleTxtSize_;
				tfm2.font=titleTxtFont_;
				titleTxt.defaultTextFormat=tfm2;
				
				titleTxt.textColor=titleTxtColor_;
				titleTxt.text="标题文字";
				titleTxt.y=5;
				titleTxt.x=5;
				skin_.addChild(titleTxt);
				
				var tfm:TextFormat=new TextFormat;
				tfm.size=txtSize_;
				tfm.font=txtFont_;
				txt.defaultTextFormat=tfm;
				
				txt.textColor=txtColor_;
				txt.text="默认内容文字";
				txt.width=txtW_;
				txt.height=txt.textHeight+6;
				skin_.addChild(txt);
			}else{
				if(titleTxt){
					titleTxt.selectable=false;
					titleTxt.autoSize=TextFieldAutoSize.LEFT;
					titleTxt.mouseWheelEnabled=false;
					titleTxt.mouseEnabled=false;
					titleTxt.wordWrap=true;
					titleTxt.text=titleStr_;
					titleTxt.width=titleTxt.textWidth+6;
				}
				
				if(txt){
					txtW_=txt.width;
					txt.textColor=txtColor_;
					txt.autoSize=txtAutoSize_;
					txt.selectable=txtSelectEnable_;
					txt.mouseEnabled=txtSelectEnable_;
					txt.mouseWheelEnabled=false;
					txt.wordWrap=true;
					txt.text=str_;
					txt.height=txt.textHeight+6;
				}
			}
		}
		
		private function resize():void
		{
			if(icon_)
			{
				iconRegPoint=DisplayObjectUtil.getRegPoint(icon_);
				
				switch(iconAlign_){
					case ALIGN_TOP:
						icon_.y=TOP_GAP+iconRegPoint.y;
						txt.y=icon_.height+TOP_GAP+AROUND_GAP;
						drawBG();
						PopUpUtils.X_CenterInParent(icon_);
						
						if(useDefaultSkin_){
							PopUpUtils.X_CenterInParent(txt);
						}
						break;
					case ALIGN_BOTTOM:
						icon_.y=txt.height+AROUND_GAP+TOP_GAP+iconRegPoint.y;
						drawBG();
						
						if(useDefaultSkin_){
							txt.y=TOP_GAP;
							PopUpUtils.X_CenterInParent(txt);
						}
						
						PopUpUtils.X_CenterInParent(icon_);
						break;
					case ALIGN_LIFT:
						icon_.x=AROUND_GAP+iconRegPoint.x;
						txt.x=AROUND_GAP+icon_.width+AROUND_GAP;
						txt.y=TOP_GAP;
						drawBG();
						break;
					case ALIGN_RIGHT:
						
						if(useDefaultSkin_){
							txt.x=AROUND_GAP;
							txt.y=TOP_GAP;
						}
						
						icon_.x=txt.x+AROUND_GAP+txt.width+iconRegPoint.x;
						drawBG();
						break;
				}
			}else{
				drawBG();
				
				if(useDefaultSkin_){
					txt.x=AROUND_GAP;
					txt.y=TOP_GAP;
					PopUpUtils.X_CenterInParent(txt);
				}
			}
			
			if(useDefaultSkin_)
			{
				closeBtn.x=BG.width-29;
				closeBtn.y=9;
			}else{
				if(BG){
					closeBtn.x=BG.width-29;
				}
			}
			
			if(titleTxtPoint_){
				titleTxt.x=titleTxtPoint_.x;
				titleTxt.y=titleTxtPoint_.y;
			}else{
				if(titleTxtAlign_==ALIGN_LIFT){
					titleTxt.x=5;
					titleTxt.y=5;
				}else if(titleTxtAlign_==ALIGN_CENTER){
					titleTxt.autoSize=TextFieldAutoSize.CENTER;
					titleTxt.x=BG.width/2-titleTxt.width/2;
				}
			}
		}
		
		private function drawBG():void
		{
			var btnH:Number=30;
			var bgW:Number=0;
			var bgH:Number=0;
			
			if(icon_){
				var tempW:Number=txt.width-icon_.width;
				var tempH:Number=txt.height-icon_.height;
				
				if(iconAlign_==ALIGN_BOTTOM || iconAlign_==ALIGN_TOP){
					if(tempW>0){
						bgW=txt.width+AROUND_GAP*2;
						bgH=TOP_GAP+txt.height+icon_.height+AROUND_GAP*3+btnH;
					}else{
						bgW=icon_.width+AROUND_GAP*2;
						bgH=TOP_GAP+txt.height+icon_.height+AROUND_GAP*3+btnH;
					}
				}
				
				if(iconAlign_==ALIGN_LIFT || iconAlign_==ALIGN_RIGHT){
					if(tempH>0){
						bgW=icon_.width+txt.width+AROUND_GAP*3;
						bgH=txt.height+TOP_GAP+AROUND_GAP*2+btnH;
					}else{
						icon_.y=TOP_GAP;
						bgW=icon_.width+txt.width+AROUND_GAP*3;
						bgH=icon_.height+TOP_GAP+AROUND_GAP*2+btnH;
					}
				}
			}else{
				bgW=txt.width+AROUND_GAP*2;
				bgH=txt.height+TOP_GAP+AROUND_GAP*2+btnH;
			}
			
			if(useDefaultSkin_)
			{
				BG=new Sprite;
				BG.graphics.lineStyle(1.5,0xffffff);
				BG.graphics.beginFill(defaultBGColor_,defaultAlpha_);
				BG.graphics.drawRoundRect(0,0,bgW,bgH,20);
				BG.graphics.endFill();
				skin_.addChildAt(BG,0);
			}else{
				BG.width=bgW;
				BG.height=bgH;
			}
			
			if(icon_){
				if(iconAlign_==ALIGN_LIFT || iconAlign_==ALIGN_RIGHT)
				{
					if(txt.height>icon_.height){
						PopUpUtils.Y_CenterInParent(icon_);
						if(useDefaultSkin_)
						{
							icon_.y+=TOP_GAP+iconRegPoint.y;
						}
					}
				}
			}
		}
		
		
		private function onClose(e:MouseEvent):void
		{
			if(closeFun_!=null){
				closeFun_();
			}
			remove();
		}
		
		private function createOneDefaultBtn():MovieClip
		{
			var m:MovieClip=new MovieClip;
			m.graphics.lineStyle(1.5,0xffffff);
			m.graphics.beginFill(defaultBtnColor_);
			m.graphics.drawRoundRect(0,0,80,30,12);
			m.buttonMode=true;
			var t:TextField=new TextField;
			t.textColor=0xffffff;
			t.mouseEnabled=false;
			t.y=4;
			m.addChild(t);
			setDefaultBtnTxt(InteractiveObject(m));
			return m;
		}
		
		private function setDefaultBtnTxt(m:InteractiveObject,str:String="默认"):void
		{
			if(m is DisplayObjectContainer)
			{
				var t:TextField=DisplayObjectContainer(m).getChildAt(0) as TextField;
				t.text=str;
				t.width=t.textWidth+6;
				t.height=t.textHeight+6;
				PopUpUtils.centerInParent(t);
			}
		}
		
		private function onMouseDown(e:MouseEvent):void
		{
			Sprite(skin_).startDrag();
		}
		
		private function onMouseUp(e:MouseEvent):void
		{
			Sprite(skin_).stopDrag();
		}
		
		private function onBtnClick(e:MouseEvent):void
		{
			var b:InteractiveObject=e.currentTarget as InteractiveObject;
			var index:uint=btnArr_.indexOf(b);
			
			if(index!=-1){
				if(btnFunArr_){
					var f:Function=btnFunArr_[index];
					if(f!=null){
						f();
					}
				}
			}
			remove();
		}
		
		public function remove():void
		{
			for(var i:int=0;i<btnArr_.length;i++){
				var b:InteractiveObject=btnArr_[i];
				if(b){
					b.removeEventListener(MouseEvent.CLICK,onBtnClick);
				}
			}
			
			if(closeBtn){
				closeBtn.removeEventListener(MouseEvent.CLICK,onClose);
				closeBtn=null;
			}
			
			this.removeEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
			this.removeEventListener(MouseEvent.MOUSE_UP,onMouseUp);
			
			btnTxtArr_=[];
			btnArr_=[];
			btnFunArr_=[];
			this.visible=false;
			DisplayObjectUtil.removeFromParent(skin_);
			DisplayObjectUtil.removeFromParent(this);
			
			if(icon_){
				DisplayObjectUtil.removeFromParent(icon_);
				icon_=null;
			}
			
			skinClass_=null;
			container_=null;
			btnContainer_=null;
			BG=null;
			closeFun_=null;
		}
	}
}